﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace TeamTasker.Client.Presentation.Controls.DockingTab
{
    [TemplatePart(Name = "Button_PART", Type = typeof (ToggleButton))]
    [TemplatePart(Name = "Border_PART", Type = typeof (Border))]
    public class DockingTab : HeaderedContentControl
    {
        public static readonly DependencyProperty DockingDirectionProperty =
            DependencyProperty.Register("DockingDirection", typeof (DockingDirection),
                                        typeof (DockingTab),
                                        new UIPropertyMetadata(DockingDirection.Left));

        public static readonly DependencyProperty IsExpandedProperty =
            DependencyProperty.Register("IsExpanded", typeof (bool), typeof (DockingTab),
                                        new FrameworkPropertyMetadata(true)
                                            {
                                                AffectsArrange = true,
                                                AffectsMeasure = true,
                                                AffectsRender = true,
                                                BindsTwoWayByDefault = true
                                            });

        public static readonly DependencyProperty IsMouseOverTabProperty =
            DependencyProperty.Register("IsMouseOverTab", typeof (bool), typeof (DockingTab),
                                        new UIPropertyMetadata(false));

        private ToggleButton _Button;
        private Border _OuterBorder;

        static DockingTab()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof (DockingTab),
                                                     new FrameworkPropertyMetadata(typeof (DockingTab)));
        }

        public bool IsExpanded
        {
            get { return (bool) GetValue(IsExpandedProperty); }
            set { SetValue(IsExpandedProperty, value); }
        }

        public DockingDirection DockingDirection
        {
            get { return (DockingDirection) GetValue(DockingDirectionProperty); }
            set { SetValue(DockingDirectionProperty, value); }
        }

        public bool IsMouseOverTab
        {
            get { return (bool) GetValue(IsMouseOverTabProperty); }
            set { SetValue(IsMouseOverTabProperty, value); }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            if (_OuterBorder != null)
            {
                _OuterBorder.MouseEnter -= DockingTab_MouseEnter;
                _OuterBorder.MouseLeave -= DockingTab_MouseLeave;
                _OuterBorder.MouseMove -= DockingTab_MouseMove;
            }
            _OuterBorder = Template.FindName("Border_PART", this) as Border;
            if (_OuterBorder != null)
            {
                _OuterBorder.MouseEnter += DockingTab_MouseEnter;
                _OuterBorder.MouseLeave += DockingTab_MouseLeave;
                _OuterBorder.MouseMove += DockingTab_MouseMove;
            }
            var button = Template.FindName("Button_PART", this) as ToggleButton;
            if (button != null)
            {
                _Button = button;
                _Button.Checked += button_Checked;
                _Button.Unchecked += button_Checked;
            }
            else
            {
                if (_Button != null)
                {
                    _Button.Checked -= button_Checked;
                    _Button.Unchecked -= button_Checked;
                }
            }
        }

        private void button_Checked(object sender, RoutedEventArgs e)
        {
            IsExpanded = _Button.IsChecked.GetValueOrDefault(false);
        }

        private void DockingTab_MouseMove(object sender, MouseEventArgs e)
        {
            IsMouseOverTab = true;
        }

        private void DockingTab_MouseLeave(object sender, MouseEventArgs e)
        {
            IsMouseOverTab = false;
        }

        private void DockingTab_MouseEnter(object sender, MouseEventArgs e)
        {
            IsMouseOverTab = true;
        }
    }
}